package com.lanou.moving.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lanou.moving.R;
import com.lanou.moving.activity.WebViewActivity;
import com.lanou.moving.adapter.PrefectureAdapter;
import com.lanou.moving.base.BaseFragment;
import com.lanou.moving.constant.URL;
import com.lanou.moving.entity.PrefectureRoot;
import com.lanou.moving.volley.GsonRequest;
import com.lanou.moving.volley.StringRequestUtf;
import com.lanou.moving.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/11/3.
 */
public class PrefectureFragment extends BaseFragment implements AdapterView.OnItemClickListener, View.OnClickListener {
    private ListView prefecture_listView;
    private PrefectureAdapter adapter;
    private List<PrefectureRoot.DataEntity> entities;
    private VolleySingleton volleySingleton;
    private GsonRequest<PrefectureRoot> gsonRequest;
    private ImageView prefecture_back;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_prefecure, null);
        volleySingleton = VolleySingleton.getInstance(mContext);
        prefecture_listView = (ListView) view.findViewById(R.id.prefecture_listView);
        prefecture_listView.setOnItemClickListener(this);
        prefecture_back = (ImageView) view.findViewById(R.id.prefecture_back);
        prefecture_back.setOnClickListener(this);
        getPrefecture();
        return view;

    }

    public void getPrefecture() {


        StringRequestUtf stringRequestUtf = new StringRequestUtf(URL.URL_ACTIVITY, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                PrefectureRoot prefectureRoot = gson.fromJson(response, PrefectureRoot.class);
                entities = prefectureRoot.getData();
                adapter = new PrefectureAdapter(getActivity(), entities);
                Log.i("entities", "----->" + entities.get(0).getName());
                prefecture_listView.setAdapter(adapter);
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i("hehe", error + "");
            }
        });
        volleySingleton.addQueue(stringRequestUtf);
    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        PrefectureRoot.DataEntity entity = (PrefectureRoot.DataEntity) parent.getItemAtPosition(position);
        Intent intent = new Intent(getActivity(), WebViewActivity.class);
        intent.putExtra("music_theme", entity);
        startActivity(intent);


    }
}
